# Maintainer: Alexey Pavlov <Alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=icu
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}" "${MINGW_PACKAGE_PREFIX}-${_realname}-debug-libs")
pkgver=57.1
pkgrel=1
arch=('any')
pkgdesc="International Components for Unicode library (mingw-w64)"
arch=('any')
url="http://www.icu-project.org/"
license=('custom:icu')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
# There's no point adding 'debug' to options as both
# debug and release packages are built regardless.
options=('!buildflags' 'staticlibs' 'strip')
source=("http://download.icu-project.org/files/icu4c/${pkgver}/icu4c-${pkgver//./_}-src.tgz"
        0004-move-to-bin.mingw.patch
        0007-actually-move-to-bin.mingw.patch
        0008-data-install-dir.mingw.patch
        0009-fix-bindir-in-config.mingw.patch
        0010-msys-rules-for-makefiles.mingw.patch
        0011-sbin-dir.mingw.patch
        0012-libprefix.mingw.patch
        0014-mingwize-pkgdata.mingw.patch
        0015-debug.mingw.patch
        0016-icu-pkgconfig.patch
        0017-icu-config-versioning.patch)
sha256sums=('ff8c67cb65949b1e7808f2359f2b80f722697048e90e7cfc382ec1fe229e9581'
            '6c7108c066ce83ff9fbb191ef090ef8db51b2bc1194b72570071dbc0a155a935'
            'add3a6805fa147384e517e78e7a72370a2b0cd51defefaa00038d40fb4ee9e25'
            '2038881890542797fe1fe9a384cf2bb6c5504b18fb26eab1368f5cb0e6f7f7af'
            'b30a05dbd9e8c70f1c3f6e119fa386925200a31cc9cab327b7affd6e07572842'
            '2adfd968379888d919f0dd4e0379cef7b744ba56d98216b82372ed4cd00a2592'
            'b05d8f1700904454329402178366ce2dd254d9afa76dc8239bfc126d7ff42800'
            'be245f2eef854859c656b797bedd9bec5d79ae41346b7b945d3c1b84aa281c61'
            'ac63e11e11fb7c849fbeff0a0008a49942a1e19ffe29385aa70248614c7ffd4d'
            'd1745b9d69729cb318cc329847160b50129621f98774a640726a7affd4b2bfd2'
            'f1d135ccd21ec1ef79e3dd2489ec48314752658433470e597d98551993aa1108'
            'fd022a7e2ae561e3158e8bb2202b675413929ab524704a1d11bb71c69a2b6722')
prepare() {
  cd "${srcdir}/icu"

  patch -p1 -i "$srcdir"/0004-move-to-bin.mingw.patch
  patch -p1 -i "$srcdir"/0007-actually-move-to-bin.mingw.patch
  patch -p1 -i "$srcdir"/0008-data-install-dir.mingw.patch
  patch -p1 -i "$srcdir"/0009-fix-bindir-in-config.mingw.patch
  patch -p1 -i "$srcdir"/0010-msys-rules-for-makefiles.mingw.patch
  patch -p1 -i "$srcdir"/0011-sbin-dir.mingw.patch
  patch -p1 -i "$srcdir"/0012-libprefix.mingw.patch
  patch -p1 -i "$srcdir"/0014-mingwize-pkgdata.mingw.patch
  patch -p1 -i "$srcdir"/0015-debug.mingw.patch
  patch -p1 -i "$srcdir"/0016-icu-pkgconfig.patch
  patch -p1 -i "$srcdir"/0017-icu-config-versioning.patch

  cd source
  autoreconf -vfi
}

build() {
  local -a extra_config
  cd "${srcdir}/icu/"
  # For ICU we ignore the options for debug above and always
  # build both debug and release (and static and shared).
  # This is because ICU people want the debug libraries be
  # suffixed with 'd' and e.g. Qt-Project go along with this
  # (though only for the static variant, oddly, see:
  #   qtbase/config.tests/unix/icu/icu.pro
  for _variant in debug release; do
    [ -d build-${CARCH}-${_variant} ] && rm -rf build-${CARCH}-${_variant}
    if [ "${_variant}" = "debug" ]; then
      extra_config=( --enable-debug )
      extra_config+=( --with-library-suffix=d )
    else
      extra_config=( --enable-release )
    fi
    cp -rf source build-${CARCH}-${_variant}
    pushd build-${CARCH}-${_variant}
    ./configure \
      --prefix=${MINGW_PREFIX} \
      --build=${MINGW_CHOST} \
      --host=${MINGW_CHOST} \
      --target=${MINGW_CHOST} \
      --disable-rpath \
      --enable-shared \
      --enable-static \
      "${extra_config[@]}"
    make -j1
    popd
  done
}

check() {
  for _variant in debug release; do
    pushd "${srcdir}/icu/build-${CARCH}-${_variant}"
    PATH=${srcdir}/icu/build-${CARCH}/bin:${srcdir}/icu/build-${CARCH}/lib:$PATH \
    make V=1 -k check
    popd
  done
}

package_icu() {
  pushd "${srcdir}/icu/build-${CARCH}-release"
  make install DESTDIR="${pkgdir}"

  mv "${pkgdir}${MINGW_PREFIX}"/lib/*.dll "${pkgdir}${MINGW_PREFIX}"/bin/
  mv "${pkgdir}${MINGW_PREFIX}"/bin/*.a "${pkgdir}${MINGW_PREFIX}"/lib/
  pushd "${pkgdir}${MINGW_PREFIX}"/bin > /dev/null
    # fix installation directory
    mv libicu*${pkgver%%.*}.dll "${pkgdir}${MINGW_PREFIX}"/lib/
    # remove unversioned DLL symlinks
    rm -f *.dll
    # move dlls back to bin
    mv "${pkgdir}${MINGW_PREFIX}"/lib/*.dll "${pkgdir}${MINGW_PREFIX}"/bin/
  popd > /dev/null
}

package_icu-debug-libs() {
  options=('debug' '!strip')
  pushd "${srcdir}/icu/build-${CARCH}-debug"
  make install DESTDIR="${pkgdir}"

  mv "${pkgdir}${MINGW_PREFIX}"/lib/*.dll "${pkgdir}${MINGW_PREFIX}"/bin/
  mv "${pkgdir}${MINGW_PREFIX}"/bin/*.a "${pkgdir}${MINGW_PREFIX}"/lib/
  pushd "${pkgdir}${MINGW_PREFIX}"/bin > /dev/null
    # fix installation directory
    mv libicu*${pkgver%%.*}.dll "${pkgdir}${MINGW_PREFIX}"/lib/
    # remove unversioned DLL symlinks
    rm -f *.dll
    # move dlls back to bin
    mv "${pkgdir}${MINGW_PREFIX}"/lib/*.dll "${pkgdir}${MINGW_PREFIX}"/bin/
  popd > /dev/null

  # Remove bits that are also in the main package.
  # TODO :: Maybe move the pkg-config files to 'd' suffixed variants?
  rm -rf "${pkgdir}${MINGW_PREFIX}"/{include,sbin,share,lib/pkgconfig}
  rm -rf "${pkgdir}${MINGW_PREFIX}"/bin/*.exe
  rm -rf "${pkgdir}${MINGW_PREFIX}"/bin/icu-config
}

package_mingw-w64-i686-icu() {
  package_icu
}

package_mingw-w64-i686-icu-debug-libs() {
  package_icu-debug-libs
}

package_mingw-w64-x86_64-icu() {
  package_icu
}

package_mingw-w64-x86_64-icu-debug-libs() {
  package_icu-debug-libs
}
